/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmppbot.parse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.muc.MultiUserChat;
import xmppbot.Main;
import xmppbot.db;

/**
 *
 * @author lian
 */
abstract public class packet {
    protected options opts = null;
    protected db MySQLDB = null;
    
    protected void init () {
        MySQLDB = Main.Connect.mysqldb;
    }
    
    public void ansver (String ansver) throws SQLException, XMPPException {
        Chat anschat;
        MultiUserChat mchat = opts.getMchat();
        if (ansver == null) {
            ansver = opts.getAnsver();
        }
        String ansmsg = opts.getAnsMsgType();
        if (ansmsg != null && opts.getAnsver() != null) {
            if (ansmsg.equals("chat")) {
                anschat = opts.getConnect().getChatManager().createChat(opts.getFrom(), new MessageListener() {
                    public void processMessage(Chat arg0, Message arg1) {}});
                anschat.sendMessage(ansver);
            } if (ansmsg.equals("groupchat")) {
                mchat.sendMessage(ansver);
            } if (ansmsg.equals("private")) {
                anschat = mchat.createPrivateChat(opts.getFrom(), new MessageListener() {
                    public void processMessage(Chat arg0, Message arg1) {}});
                anschat.sendMessage(ansver);
            } if (ansmsg.equals("gprivate")) {
                mchat.sendMessage(opts.getShortFrom() + ": " + ansver);
            }
        }
    }
    
    @SuppressWarnings("deprecation")
    public void log () throws SQLException {
        String AnsMsgType;
        if (opts.getAnsMsgType() == null) {
            AnsMsgType = "null";
        } else {
            AnsMsgType = "'" + opts.getAnsMsgType() + "'";
        }
        int executableint;
        int ansverableint;
        if (opts.getExecutable()) {
            executableint = 1;
        } else {
            executableint = 0;
        }
        if (opts.getAnsverable()) {
            ansverableint = 1;
        } else {
            ansverableint = 0;
        }
        String funcname;
        if (opts.getFuncName() == null) {
            funcname = "null";
        } else {
            funcname = opts.getFuncId();
        }
        String ansver;
        if (opts.getAnsver() == null) {
            ansver = "null";
        } else {
            ansver = "'" + opts.getAnsver() + "'";
        }
        //System.out.println("552");
        String confname;
        if (!opts.getMsgType().equals("chat")) {
            ResultSet rs = MySQLDB.getStatement().executeQuery("SELECT id FROM conferences WHERE conference = '" + utils.parseSymbols(opts.getMchat().getRoom()) + "';");
            rs.next();
            confname = rs.getString("id");
            db.killrs(rs); rs = null;
        } else {
            confname = "null";
        }
        int id = 0;
        ResultSet rs = MySQLDB.getStatement().executeQuery("SELECT id FROM allusers WHERE user = '" + utils.parseSymbols(opts.getFrom()) + "'");
        if (rs.next()) {
            id = rs.getInt("id");
        } else {
            db.killrs(rs); rs = null;
            if (!opts.getMsgType().equals("chat")) {
                MySQLDB.getStatement().executeUpdate("INSERT INTO allusers VALUES (null,'" + utils.parseSymbols(opts.getFrom()) + "','" + utils.parseSymbols(opts.getShortFrom()) + "',(SELECT id FROM conferences WHERE login = '" + utils.parseSymbols(opts.getMchat().getNickname()) + "' AND conference = '" + utils.parseSymbols(opts.getMchat().getRoom()) + "'),0);");
            } else {
                MySQLDB.getStatement().executeUpdate("INSERT INTO allusers VALUES (null,'" + utils.parseSymbols(opts.getFrom()) + "','" + utils.parseSymbols(opts.getShortFrom()) + "',null,0);");
            }
            rs = MySQLDB.getStatement().executeQuery("SELECT id FROM allusers WHERE user = '" + utils.parseSymbols(opts.getFrom()) + "'");
            rs.next();
            id = rs.getInt("id");
        }
        MySQLDB.getStatement().executeUpdate("UPDATE allusers SET msgs = msgs+1 WHERE user = '" + utils.parseSymbols(opts.getFrom()) + "';");
        db.killrs(rs); rs = null;
        System.out.println(opts.getMsgType() + "\t\t" + opts.getFrom() + "\t\t" + opts.getMsg());
//        Message msg = new Message("lian@jabbus.org");
//        msg.setType(Message.Type.chat);
//        msg.setBody(opts.getMsgType() + "\t\t" + opts.getFrom() + "\t\t" + opts.getMsg());
//        Main.Connect.Connect.sendPacket(msg);
        Date date = opts.getDate();
        MySQLDB.getStatement().executeUpdate("INSERT INTO messagelog VALUES (null,'" + (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "','" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "'," + id + ",'" + utils.parseSymbols(opts.getShortFrom()) + "','" + utils.parseSymbols(opts.getMsg()) + "',(SELECT id FROM sourcevals WHERE val = '" + opts.getMsgType() + "'),(SELECT id FROM sourcevals WHERE val = '" + opts.getAnsMsgType() + "')," + executableint + "," + funcname + "," + ansverableint + "," + ansver + "," + utils.parseSymbols(confname) + ");");
    }
     
    abstract public void process () throws SQLException;
}
